/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { BaseElement, element } from '../BaseElement';
import { getParseDateStr } from './LitDatePicker';
import { LitDatePane } from './LitDatePane';

@element('lit-date-rang-pane')
export class LitDateRangePane extends BaseElement {
  private selected: boolean = false;
  private leftDatePicker: LitDatePane | undefined;
  private rightDatePicker: LitDatePane | undefined;
  private dateV: any;
  private date: Array<Date> = [new Date(), new Date()];

  static get observedAttributes() {
    return ['minDate', 'maxDate', 'dateType'];
  }

  get defaultDateValue() {
    let defaultValue = this.getAttribute('defaultDateValue');
    return defaultValue ? defaultValue.split('~') : [new Date(), new Date()];
  }

  set defaultDateValue(defaultValue: any) {
    this.leftDatePicker!.setRangeDate(defaultValue);
    this.rightDatePicker!.setRangeDate(defaultValue);
    this.setAttribute('defaultDateValue', defaultValue.join('~'));
  }

  get value(): any {
    return this.leftDatePicker!.rangeDate.map((val: any) => {
      return getParseDateStr(val, this.dateType);
    });
  }

  set value(dateValue: any) {
    if (getParseDateStr(dateValue[0]) > getParseDateStr(dateValue[1])) {
      [dateValue[0], dateValue[1]] = [dateValue[1], dateValue[0]];
    }
    this.dateV = dateValue;
    this.renderDate(dateValue);
    this.rightDatePicker!.renderDate(dateValue[1]);
    this.leftDatePicker!.renderDate(dateValue[0]);
  }

  get minDate() {
    return this.getAttribute('minDate') || '';
  }

  set minDate(minvalue: string) {
    this.setAttribute('minDate', minvalue);
    if (this.leftDatePicker) {
      this.leftDatePicker!.setAttribute('minDate', minvalue);
    }
    if (this.rightDatePicker) {
      this.rightDatePicker!.setAttribute('minDate', minvalue);
    }
  }

  get maxDate() {
    return this.getAttribute('maxDate') || '';
  }

  set maxDate(maxValue: string) {
    this.setAttribute('maxDate', maxValue);
    if (this.leftDatePicker) {
      this.leftDatePicker!.setAttribute('maxDate', maxValue);
    }
    if (this.rightDatePicker) {
      this.rightDatePicker!.setAttribute('maxDate', maxValue);
    }
  }

  get dateType() {
    return this.getAttribute('dateType') || 'date';
  }

  set dateType(typeValue) {
    this.setAttribute('dateType', typeValue);
  }

  initElements(): void {
    this.leftDatePicker = this.shadowRoot?.getElementById('left-datepicker') as LitDatePane;
    this.rightDatePicker = this.shadowRoot?.getElementById('right-datepicker') as LitDatePane;
    this.dateType = this.dateType;
    this.minDate && (this.minDate = this.minDate);
    this.maxDate && (this.maxDate = this.maxDate);
    this.value = this.defaultDateValue;
    this.leftDatePicker!.addEventListener('date-select', (event: Event) => {
      // @ts-ignore
      if (event.detail.value) {
        // @ts-ignore
        this.chooseDate(event.detail.value);
      }
    });
    this.rightDatePicker!.addEventListener('date-select', (event) => {
      // @ts-ignore
      if (event.detail.value) {
        // @ts-ignore
        this.chooseDate(event.detail.value);
      }
    });
  }

  chooseDate(dateValue: any) {
    if (dateValue) {
      if (!this.selected) {
        this.date![0] = new Date(dateValue);
        this.date![1] = new Date(dateValue);
      } else {
        this.date![1] = new Date(dateValue);
        if (this.date![0] > this.date![1]) {
          [this.date![0], this.date![1]] = [this.date![1], this.date![0]];
        }
      }
      this.renderDate(this.date, this.selected);
      this.selected = !this.selected;
    }
  }

  renderDate(dateValue: any, isChange?: any) {
    this.leftDatePicker!.setRangeDate(dateValue);
    this.rightDatePicker!.setRangeDate(dateValue);
    if (isChange) {
      this.value = dateValue;
    }
  }

  initHtml(): string {
    return ` 
        <style>
            :host{
                display: inline-flex;
                font-size: 14px;
            }
            #left-datepicker, #right-datepicker{
                font-size: inherit;
                border: 0;
            }
            
            #right-datepicker {
                margin-left: 1.2em;
                border: 0;
            }
        </style>
        <lit-date-pane id="left-datepicker" range="left"></lit-date-pane>
        <lit-date-pane id="right-datepicker" range="right"></lit-date-pane>`;
  }

  attributeChangedCallback(name: string, oldValue: string, newValue: string) {
    if (!this.leftDatePicker || newValue == null) {
      return;
    }
    switch (name) {
      case 'minDate':
        this.leftDatePicker!.minDate = newValue;
        this.rightDatePicker!.minDate = newValue;
        break;
      case 'maxDate':
        this.leftDatePicker!.maxDate = newValue;
        this.rightDatePicker!.maxDate = newValue;
        break;
      case 'dateType':
        this.leftDatePicker!.dateType = newValue;
        this.rightDatePicker!.dateType = newValue;
        break;
      default:
        break;
    }
  }
}
